package com.zhao.commonUtil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * Description: <br/>
 * Copyright (c) ，2021 ， 赵 <br/>
 * This program is protected by copyright laws. <br/>
 * Date： 2021/1/5 17:12
 *
 * @author 柒柒
 * @version : 1.0
 * @Update [序号][日期YYYY-MM-DD] [更改人姓名][变更描述]
 */

public class JsonParser {

    private JSONObject jsonObject;

    /**
     * 无参构造方法
     */
    private JsonParser(){

    }

    /**
     * 有参构造方法
     * @param jsonStr
     */
    public JsonParser(String jsonStr) {
        this.jsonObject = JSONObject.parseObject(jsonStr);
    }

    /**
     * 解析json字符串
     * @param jsnStr
     * @return
     */
    public static JSONObject parseJsonStr(String jsnStr){
        return JSONObject.parseObject(jsnStr);
    }

    /**
     * 获取字段
     * @param field
     * @return
     */
    public String getJsnField(String field){
        return jsonObject.getString(field);
    }

    /**
     * 获取第二层级下的字符
     * @param col1
     * @param col2
     * @return
     */
    public String getSCJsnField(String col1, String col2) {
        JSONObject innerObj = JSONObject.parseObject(this.jsonObject.getString(col1));
        if (innerObj != null) {
            return innerObj.getString(col2);
        }else {
            return null;
        }
    }

    /**
     * 解析json数组
     * @param jsnStr
     * @param field
     * @return
     */
    public static JSONArray getJsonArray(String jsnStr, String field) {
        JSONObject jsnObj = JSONObject.parseObject(jsnStr);
        String jsnField = jsnObj.getString(field);
        JSONArray js = JSON.parseArray(jsnField);
        return js;
    }

    public static String getJsnField(String jsnStr, String field) {
        JSONObject jsnObj = JSONObject.parseObject(jsnStr);
        String jsnField = jsnObj.getString(field);
        return jsnField;
    }

    public static String getJsnColField(String jsnStr, String colField) {
        JSONObject jsnObj = JSONObject.parseObject(jsnStr);
        JSONObject outerObj = JSONObject.parseObject(jsnObj.getString("Values"));
        JSONObject innerObj = null;
        if (outerObj != null){
            innerObj = JSONObject.parseObject(outerObj.getString("After"));
        }
        if (innerObj != null){
            return innerObj.getString(colField);
        }else {
            return null;
        }
    }

    public static String getJsnColFieldBefore(String jsnStr, String colField){
        JSONObject jsnObj = JSONObject.parseObject(jsnStr);
        JSONObject outerObj = JSONObject.parseObject(jsnObj.getString("value"));
        JSONObject innerObj = null;
        if (outerObj != null){
            innerObj = JSONObject.parseObject(outerObj.getString("Before"));
        }
        if (innerObj != null){
            return innerObj.getString(colField);
        }else{
            return null;
        }
    }

    public static String getSCJsnField(String jsnStr, String col1, String col2){
        JSONObject jsnObj = JSONObject.parseObject(jsnStr);
        JSONObject innerObj = JSONObject.parseObject(jsnObj.getString(col1));
        if (innerObj != null){
            return innerObj.getString(col2);
        }else{
            return null;
        }
    }
}













